﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class DCStoreController : BaseDataController
    {

        public ActionResult DCStore()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        ///<summary>
        ///查询前置仓仓库信息
        ///</summary>
        ///<returns></returns>
        public JsonResult InitJsonResult(string warehHuseId, string thirdid, string status)
        {
            string where = "  where 1=1 ";
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            if (warehHuseId != "" && warehHuseId != "0")
            {
                where += " and t1.WAREHOUSEID=" + warehHuseId;
            }
            if (thirdid != "" && thirdid != "0")
            {
                where += " and t1.thirdid='" + thirdid + "'";
            }
            if (status != "")
            {
                where += " and T1.status='" + status + "'";
            }
            var retData = new object();
            BDCStore db = new BDCStore();
            retData = db.GetDCStoreInfo(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }


        ///// <summary>
        ///// 获取门店下拉列表
        ///// </summary>
        ///// <returns></returns>
        //public JsonResult GetDCStoreList()
        //{
        //    string defaultValue = Request["defaultValue"].ToString();
        //    int warehouseid = Convert.ToInt32(Request["sWareHouseId"]);
        //    int thirdid = Convert.ToInt32(Request["sThirdid"]);
        //    var retData = new object();
        //    BDCStore db = new BDCStore();
        //    retData = db.GetDCDCStoreList(defaultValue, thirdid, warehouseid).ToList();
        //    return Json(retData);
        //}


        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                DCStore _DCStore = new DCStore();

                string storeID = Request["hiddenStoreID"].Trim(); ;

                _DCStore.THIRDID = Convert.ToInt32(Request["sThirdid"]);
                _DCStore.WAREHOUSEID = Convert.ToInt32(Request["sWareHouseId"]);
                _DCStore.STORENAME = Request["stxtStoreName"].ToString();
                _DCStore.THIRDSTORECODE = Request["stxtStoreCode"].ToString();
                _DCStore.STOREADDRESS = Request["txtADDRESS"].ToString();
                _DCStore.OPRNO = model.USERCODE.ToString();
                _DCStore.STATUS = Request["sstatus"].ToString();
                if (_DCStore.THIRDID == 0 || _DCStore.WAREHOUSEID == 0)
                {
                    return sjson = "{success:false,msg:'请选择仓库或者第三方名称！'}";
                }

                BDCStore db = new BDCStore();

                if (!string.IsNullOrEmpty(storeID))
                {
                    _DCStore.STOREID = int.Parse(storeID);
                }
                List<DCStore> lstDCThirdInfo = db.GetDCStoreInfoByCode(_DCStore.STORENAME, _DCStore.THIRDSTORECODE);
                if (lstDCThirdInfo != null && lstDCThirdInfo.Count > 0)
                {
                    sjson = "{success:false,msg:'此门店已经存在！'}";
                }
                else
                {
                    int n = db.MergeDCStoreInfo(_DCStore);
                    sjson = "{success:true}";
                }
                return sjson;
            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }
    }
}
